package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.service.admin.MarketBrandService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：Katherine
 * @Package：com.cskaoyan.market.service.impl
 * @Project：java-project1
 * @name：MarketBrandServiceImpl
 * @Date：2023/5/7 11:04
 * @Filename：MarketBrandServiceImpl
 */
@Service
public class MarketBrandServiceImpl implements MarketBrandService {

    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public List<MarketBrand> list(String id, String name) {

        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = brandExample.or().andDeletedEqualTo(false);

        //查询
        if (id != null) {
            int id2 = Integer.parseInt(id);
            criteria.andIdEqualTo(id2).andLogicalDeleted(true);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        brandExample.orderBy("add_time desc");

        PageHelper.startPage(1, 20);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(brandExample);

        return marketBrands;
    }

    @Override
    public List<MarketBrand> create(String name, String desc, BigDecimal bigDecimal, String picUrl) {

        //当我们执行当前对象的方法时，那么执行的便是对应的sql语句

        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(bigDecimal);
        marketBrand.setPicUrl(picUrl);
        //提交、关闭
        //记得括号里MarketBrand
        int i = brandMapper.insertSelective(marketBrand);
        List<MarketBrand> list = new ArrayList<>();
        //把marketBrand中的这些数据添加进去
        list.add(marketBrand);
        return list;
    }


    @Override
    public List<MarketBrand> update(MarketBrand brand) {

        int i = brandMapper.updateByPrimaryKeySelective(brand);

        //记得括号里MarketBrand
        List<MarketBrand> list = new ArrayList<>();
        //把marketBrand中的这些数据添加进去
        list.add(brand);

        return list;
    }

    @Override
    public int delete(Integer id) {

        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setId(id);
        //注意
        marketBrand.setDeleted(true);

        int i = brandMapper.updateByPrimaryKeySelective(marketBrand);

        return i;
    }

}
